/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.notice.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.notice.enums.AppPushSoundEnum;
import org.springblade.modules.notice.enums.PushObjectEnum;
import org.springblade.modules.notice.enums.TriggerConditionEnum;
import org.springblade.modules.notice.enums.TriggerNodeEnum;
import org.springblade.modules.notice.vo.BizMsgConfigChangStatusParam;
import org.springblade.modules.notice.vo.BizMsgConfigListParam;
import org.springblade.modules.notice.vo.CommonEnumInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.notice.entity.BizMsgConfig;
import org.springblade.modules.notice.vo.BizMsgConfigVO;
import org.springblade.modules.notice.wrapper.BizMsgConfigWrapper;
import org.springblade.modules.notice.service.IBizMsgConfigService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;
import java.util.Map;

/**
 * 业务消息管理表 控制器
 *
 * @author ysq
 * @since 2024-11-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("sida-maintain-admin/bizmsgconfig")
@Api(value = "业务消息管理表", tags = "业务消息管理表接口")
public class BizMsgConfigController extends BladeController {

	private final IBizMsgConfigService bizMsgConfigService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入bizMsgConfig")
	public R<BizMsgConfigVO> detail(BizMsgConfig bizMsgConfig) {
		BizMsgConfig detail = bizMsgConfigService.getOne(Condition.getQueryWrapper(bizMsgConfig));
		return R.data(BizMsgConfigWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 业务消息管理表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入bizMsgConfig")
	public R<IPage<BizMsgConfigVO>> list(BizMsgConfig bizMsgConfig, Query query) {
		IPage<BizMsgConfig> pages = bizMsgConfigService.page(Condition.getPage(query), Condition.getQueryWrapper(bizMsgConfig));
		return R.data(BizMsgConfigWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 业务消息管理表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入bizMsgConfig")
	public R<IPage<BizMsgConfigVO>> page(BizMsgConfigVO bizMsgConfig, Query query) {
		IPage<BizMsgConfigVO> pages = bizMsgConfigService.selectBizMsgConfigPage(Condition.getPage(query), bizMsgConfig);
		return R.data(pages);
	}

	/**
	 * 新增 业务消息管理表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入bizMsgConfig")
	public R save(@Valid @RequestBody BizMsgConfig bizMsgConfig) {
		return R.status(bizMsgConfigService.save(bizMsgConfig));
	}

	/**
	 * 修改 业务消息管理表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入bizMsgConfig")
	public R update(@Valid @RequestBody BizMsgConfig bizMsgConfig) {
		return R.status(bizMsgConfigService.customUpdate(bizMsgConfig));
	}

	/**
	 * 新增或修改 业务消息管理表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入bizMsgConfig")
	public R submit(@Valid @RequestBody BizMsgConfig bizMsgConfig) {
		return R.status(bizMsgConfigService.saveOrUpdate(bizMsgConfig));
	}

	/**
	 * 删除 业务消息管理表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(bizMsgConfigService.deleteLogic(Func.toLongList(ids)));
	}

	@GetMapping("/select/node")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "下拉选择-触发节点")
	public R<List<CommonEnumInfo>> selectNodeList() {
		return R.data(TriggerNodeEnum.getSelectList());
	}

	@GetMapping("/select/condition")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "下拉选择-触发条件")
	public R<List<CommonEnumInfo>> selectTriggerConditionList(@ApiParam(value = "业务类型") @RequestParam("businessType") Integer businessType) {
		return R.data(TriggerConditionEnum.getSelectList(businessType));
	}

	@GetMapping("/select/push/object")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "下拉选择-推送对象")
	public R<List<CommonEnumInfo>> selectPushObjectList(@ApiParam(value = "业务类型") @RequestParam("businessType") Integer businessType) {
		return R.data(PushObjectEnum.getSelectList(businessType));
	}

	@GetMapping("/select/app/push/sound")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "下拉选择-APP推送铃声")
	public R<List<Map<String, String>>> selectAppPushSoundList(@ApiParam(value = "业务类型") @RequestParam("businessType") Integer businessType) {
		return R.data(AppPushSoundEnum.getSelectList(businessType));
	}

	@TenantIgnore
	@PostMapping("update/status")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "修改状态")
	public R updateStatus(@RequestBody @Valid List<BizMsgConfigChangStatusParam> params) {
		return R.status(bizMsgConfigService.customChangeStatus(params));
	}

	@GetMapping("/getBizMsgConfigList")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "根据条件查询（优先查租户自定义数据，没有则取系统配置）")
	public R<List<BizMsgConfig>> getBizMsgConfigList(BizMsgConfigListParam param) {
		param.setTenantId(AuthUtil.getTenantId());
		return R.data(bizMsgConfigService.getBizMsgConfigList(param));
	}

	@PostMapping("/test")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "测试")
	public R test(@RequestParam("id") Long id) {
		return R.data(bizMsgConfigService.test(id));
	}
}
